package com.demo;

import com.demo.util.EmailSender;
import com.fasterxml.jackson.core.type.TypeReference;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.demo.model.Project;
import com.demo.model.Receiver;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.mail.MessagingException;
import java.io.File;
import java.io.IOException;
import java.util.List;

public class BulkEmailSender {
    private static final Logger logger = LogManager.getLogger(BulkEmailSender.class);

    public static void main(String[] args) throws MessagingException {
        // 读取project信息
        ObjectMapper objectMapper = new ObjectMapper();
        Project project = null;
        try {
            project = objectMapper.readValue(new File("config/project.json"), Project.class);
            logger.info("projectName: " + project.getProject_name());
            logger.info("Cc: " + project.getCcAddresses());
        } catch (IOException e) {
            logger.error("读取project信息错误", e);
        }

        // 读取receiver加载接收者列表
        List<Receiver> receiverList = null;
        try {
            receiverList = objectMapper.readValue(new File("config/receiver.json"),
                    new TypeReference<List<Receiver>>() {
            });
            for (Receiver receiver : receiverList) {
                logger.info(receiver.getCompany_name() + ": " + receiver.getEmail());
            }
        } catch (IOException e) {
            logger.error("读取文件失败：", e);
        }

        EmailSender.createEmail(project, receiverList);
        EmailSender.sendEmail();
    }
}